
var PurchaseCk = {
	dialog_add_: function (url, title, xy) {
		var result = Common.result();
		$('#dialog_add').dialog('option', 'buttons', {
			"关闭": function() {
				$(this).dialog("close");
			},

			"添加": function() {
				if (!confirm("确认是否提交？")) {
					return false;
				}

				var form = $("#form_add")[0];

				var body = { };
				
				body.customer = form.customer.value;
				body.customer_x = Common.customer_x(form);
				
				body.emp = form.emp.value;
				
				body.name = form.name.value;
				body.date = form.date.value;
				body.address = form.address.value;
				body.content = form.content.value;

				$(this).dialog("close");

				$.post('material.php?do=PurchaseCk.addPost', body, function(data) {
					$(result).html(data);
				});
			}
		});

		dialog_add(url, title, xy);
	},
	
	dialog_view_: function (url, title, xy) {
		$('#dialog_view').dialog('option', 'buttons', {
			"关闭": function() { 
				$(this).dialog("close");
			},
			
			"打印": function() { 
				$.jPrintArea("#dialog_view");
			},
			
			"另存": function() { 
				var id = $.cookie('id_');
                var url = 'material.php?do=PurchaseCk.test&tpl=doc&id=' + id; 	 

                jswin(url, 'purchase_print', '800x600');
			}
		});

		dialog_view(url, title, xy);
	},

	popup: function (e, p2, id, out, status) {
		$.cookie('id_', id);
		
		p2 = query(p2);
		$.cookie('p2', p2);
		
		var c1 = 'material.php?do=PurchaseCk.edit&id=' + id;
		var c2 = 'material.php?do=PurchaseCk.view&id=' + id;
		
		var c3 = 'material.php?do=PurchaseCk.status&id=' + id + p2;
		var c4 = 'material.php?do=PurchaseCk.reason&id=' + id;
				
		var a1 = ['编辑信息', 'url', 'PurchaseCk.dialog_edit_', '800x500'];
		var a2 = ['查看信息', 'url', 'Common.dialog_view_', '800x500'];
				
		// var a5 = ['暂停采购', 'url', 'PurchaseCk.dialog_stop', '800x500'];
		var a6 = ['取消采购', 'url', 'PurchaseCk.dialog_cancel', '800x500'];
		
		var c8 = 'material.php?do=PurchaseCk.in&id=' + id;		
		var a8 = ['采购入库', 'url', 'PurchaseCk.dialog_in', '800x500'];
		
		// 等待入库
		if (status == 3 || status == 4 || status == 8) {
			
			var s = c8 + ", " + c2;

			return this.popup_(e, [a8, a2], s);
		}
		
		if (status == 9 && (out == 0 || out == null || out == '')) {
			var d1 = 'material.php?do=PurchaseCk.out&id=' + id;		
			var b1 = ['添加支出', 'url', 'PurchaseCk.dialog_out', '640x400'];
			
			var b2 = ['进货单', 'url', 'PurchaseCk.dialog_view_', '800x500'];
			
			var s = d1 + ", " + c2 + "&view=2, " + c2;

			// return this.popup_(e, [b1, b2, a2], s);
		}

		var s = c2;

		return this.popup_(e, [a2], s);
	},	

	popup_: function (e, a, s) {
		var id = $.cookie('id_');
		
		var c2 = 'material.php?do=PurchaseCk.upload&id=' + id;
		var a2 = ['上传附件', 'url', 'Common.dialog_upload', '600x420'];
		
		var ss = 'material.php?do=PurchaseCk.ss&id=' + id;
		var bb = ['修改状态', 'url', 'PurchaseCk.dialog_ss', '400x200'];
		
		s += ", " + c2;
		
		a.push(a2);
		// a.push(bb);
		
		return popup(e, a, s);
	},

	/**
	 * 修改状态
	 */
	dialog_ss: function (url, title, xy) {
		var result = Common.result();
		$.getJSON('material.php?do=PurchaseCk.audit&item=188', function(json) {
			if (json.status == 1) {
				$('#dialog_edit').dialog('option', 'buttons', {
					"关闭": function() {
						$(this).dialog("close");
					},

					"修改": function() {
						if (!confirm("确认是否提交？")) {
							return false;
						}

						var form = $("#form_edit")[0];

						var body = {};
						body.id = form.id.value;
						body.field = form.field.value;
						body.data = form.data.value;
						body.status = form.status.value;

						$(this).dialog("close");

						var url = 'material.php?do=PurchaseCk.ssPost';
						var p2 = $.cookie('p2');
						if (p2 != null) {
							url += p2;
						}

						$.post(url, body, function(data) {
							$(result).html(data);
						});
					}
				});

				dialog_edit(url, title, xy);
			} else {
				_error();
			}
		});
	},

	dialog_view_check: function (url, title, xy) {
		$('#dialog_view').dialog('option', 'buttons', {
			"关闭": function() { 
				$(this).dialog("close");
			}
		});

		dialog_view(url, title, xy);
	},

	dialog_verify: function (url, title, xy) {
		var result = Common.result();
		$.getJSON('material.php?do=PurchaseCk.ajax_emp', function(json) {
			if (json.status == 1) {
				$('#dialog_edit').dialog('option', 'buttons', {
					"关闭": function() { 
						$(this).dialog("close");
					},		

					"不同意": function() {
						if (!confirm("确认是否不同意采购？")) {
							return false;
						}
						
						var form = $("#form_edit")[0];

						var body = {};
						body.id = form.id.value;
						body.content = form.content.value;
						body.status = 19;
						
						var url = 'material.php?do=PurchaseCk.verifyPost';
						var p2 = $.cookie('p2');
						if (p2 != null) {
							url += p2;
						}
						
						$(this).dialog("close");

						$.post(url, body, function(data) {
							$(result).html(data);
						});
					},
					
					"同意": function() {
						if (!confirm("确认是否同意采购？")) {
							return false;
						}

						var form = $("#form_edit")[0];
						
						var body = {};
						body.id = form.id.value;
						body.content = form.content.value;
						
						body.status = 2;
						
						var url = 'material.php?do=PurchaseCk.verifyPost';
						var p2 = $.cookie('p2');
						if (p2 != null) {
							url += p2;
						}
						
						$(this).dialog("close");

						$.post(url, body, function(data) {
							$(result).html(data);
						});
					}
				});

				dialog_edit(url, title, xy);
			} else {
				_error();
			}
		});	
	},

	in_validate: function () {
		var validator = $("#form_add").validate({
			rules: {
				place: {
					required: true,
					minlength: 2
				}					
			},
			messages: {
				place: {
					required: "请选择或输入仓库信息",
					minlength: $.validator.format("请输入字数不少于{0}个的仓库信息")
				}
			},
			errorPlacement: function(label, element) {
				if (/^place/.test(element[0].name)) {
					label.insertAfter("#place_t");
				} else {
					label.insertAfter(element);
				}
			},
			success: function(label) {
				label.html("&nbsp;").addClass("checked");
			}
		});

		if (!validator.form()) {
			return false;
		}

		return true;
	},

	dialog_in: function (url, title, xy) {
		var result = Common.result();
		$.getJSON('material.php?do=PurchaseCk.audit&item=63', function(json) {
			if (json.status == 1) {
				$('#dialog_add').dialog('option', 'buttons', {
					"关闭": function() { 
						$(this).dialog("close");
					},
					
					"入库": function() {
						if (!confirm("确认是否入库？")) {
							return false;
						}

						if (!PurchaseCk.in_validate()) {
							return false;
						}

						var form = $("#form_add")[0];
								
						var body = { };
						body.id = form.id.value;
						
						body.emp = form.emp.value;
						body.date = form.date.value;
						body.number = form.number.value;
						
						body.purchase = form.purchase.value;
						body.supplier = form.supplier.value;
												
						body.place = form.place.value;
						body.content = form.content.value;
						
						if (is_null(body.number)) {
							alert("请输入供应商的送货单号，送货单不能为空，若没有可以自行以日期格式编写一个即可。");
							return false;
						}

						$(this).dialog("close");
						
						var url = 'material.php?do=PurchaseCk.inPost';
						var p2 = $.cookie('p2');
						if (p2 != null) {
							url += p2;
						}

						$.post(url, body, function(data) {
							$(result).html(data);
						});
					}
				});

				dialog_add(url, title, xy);
			} else {
				_error();
			}
		});	
	},

	list_continue: function (url, view) {
		$.getJSON('material.php?do=PurchaseCk.ajax_emp', function(json) {
			if (json.status == 1) {
				if (confirm("是否继续采购？")) {
					list(url, view);
				}
			} else {
				_error();
			}
		});
	},

	cart_validate: function () {
		var validator = $("#form_edit").validate({
			rules: {
				supplier: "required",
				address: {
					required: true,
					minlength: 2
				}					
			},
			messages: {
				address: {
					required: "请输入交货地址",
					minlength: $.validator.format("请输入字数不少于{0}个的交货地址")
				},
				supplier: {
					required: "请输入供应商信息"
				}
			},
			success: function(label) {
				label.html("&nbsp;").addClass("checked");
			}
		});

		if (!validator.form()) {
			return false;
		}

		return true;
	},

	/**
	 * 开始采购
	 */
	dialog_start: function (url, title, xy) {
		var result = Common.result();
		$.getJSON('material.php?do=PurchaseCk.ajax_emp', function(json) {
			if (json.status == 1) {
				$('#dialog_edit').dialog('option', 'buttons', {
					"关闭": function() { 
						$(this).dialog("close");
					},
					
					"开始采购": function() {
						if (!confirm("确认是否开始采购？")) {
							return false;
						}

						var form = $("#form_edit")[0];
								
						var body = { };

						body.id = $.cookie('id_');
						body.status = 3;

						$(this).dialog("close");
						
						var url = 'material.php?do=PurchaseCk.start';
						var p2 = $.cookie('p2');
						if (p2 != null) {
							url += p2;
						}
						
						$.post(url, body, function(data) {
							$(result).html(data);
						});
					}
				});

				dialog_edit(url, title, xy);
			} else {
				_error();
			}
		});	
	},

	/**
	 * 采购下单
	 */
	dialog_po: function (url, title, xy) {
		var result = Common.result();
		$.getJSON('material.php?do=PurchaseCk.ajax_emp', function(json) {
			if (json.status == 1) {
				$('#dialog_edit').dialog('option', 'buttons', {
					"关闭": function() { 
						$(this).dialog("close");
					},
					
					"下单": function() {
						if (!confirm("确认是否下单？")) {
							return false;
						}

						var form = $("#form_edit")[0];
								
						var body = { };
						body.id = form.id.value;
						
						body.currency = form.currency.value;
						body.content = form.content.value;

						$(this).dialog("close");
						
						var url = 'material.php?do=PurchaseCk.poPost';
						var p2 = $.cookie('p2');
						if (p2 != null) {
							url += p2;
						}
						
						$.post(url, body, function(data) {
							$(result).html(data);
						});
					}
				});

				dialog_edit(url, title, xy);
			} else {
				_error();
			}
		});	
	},

	dialog_finish: function (url, title, xy) {
		var result = Common.result();
		$.getJSON('material.php?do=PurchaseCk.ajax_emp', function(json) {
			if (json.status == 1) {
				$('#dialog_edit').dialog('option', 'buttons', {
					"关闭": function() { 
						$(this).dialog("close");
					},
					
					"完成采购": function() {
						if (!confirm("确认是否提交？")) {
							return false;
						}

						var form = $("#form_edit")[0];
								
						var body = { };

						body.id = $.cookie('id_');
						body.status = 8;

						$(this).dialog("close");
						
						var url = 'material.php?do=PurchaseCk.finish';
						var p2 = $.cookie('p2');
						if (p2 != null) {
							url += p2;
						}
						
						$.post(url, body, function(data) {
							$(result).html(data);
						});
					}
				});

				dialog_edit(url, title, xy);
			} else {
				_error();
			}
		});	
	},
	
	/**
	 * 编辑
	 */
	dialog_edit_: function (url, title, xy) {
		var result = Common.result();
		$.getJSON('material.php?do=PurchaseCk.ajax_emp&item=188', function(json) {
			if (json.status == 1) {
				$('#dialog_edit').dialog('option', 'buttons', {
					"关闭": function() {
						$(this).dialog("close");
					},

					"修改": function() {
						if (!confirm("确认是否修改？")) {
							return false;
						}

						var form = $("#form_edit")[0];

						var body = { };
						body.id = form.id.value;
						
						body.customer = form.customer.value;
						body.customer_x = Common.customer_x(form);
						
						body.supplier = form.supplier.value;
						body.supplier_x = Supplier.supplier_x(form);
						
						body.currency = form.currency.value;
						body.type = form.type.value;
						body.tax = form.tax.value;
						
						body.emp = form.emp.value;
						
						body.name = form.name.value;
						body.date = form.date.value;						
						body.address = form.address.value;
						body.content = form.content.value;
						
						body.log = data;

						$(this).dialog("close");
						
						var url = 'material.php?do=PurchaseCk.editPost';
						var p2 = $.cookie('p2');
						if (p2 != null) {
							url += p2;
						}

						$.post(url, body, function(data) {
							$.cookie('cart2', null);

							$(result).html(data);
						});
					}
				});

				dialog_edit(url, title, xy);
			} else {
				_error();
			}
		});	
	},

	dialog_supplier: function (url, title, xy) {
		var result = Common.result();
		$.getJSON('material.php?do=PurchaseCk.ajax_emp', function(json) {
			if (json.status == 1) {
				$('#dialog_edit').dialog('option', 'buttons', {
					"关闭": function() {
						$(this).dialog("close");
					},

					"确定": function() {
						if (!confirm("确认是否提交？")) {
							return false;
						}

						var form = $("#form_edit")[0];

						var body = {};
						body.id = form.id.value;
						body.supplier = form.supplier.value;
						body.supplier_x = Supplier.supplier_x(form);
						
						body.currency = form.currency.value;
						body.type = form.type.value;
						body.tax = form.tax.value;
						body.content = form.content.value;

						$(this).dialog("close");

						var url = 'material.php?do=PurchaseCk.supplierPost';
						var p2 = $.cookie('p2');
						if (p2 != null) {
							url += p2;
						}

						$.post(url, body, function(data) {
							$(result).html(data);
						});
					}
				});

				dialog_edit(url, title, xy);
			} else {
				_error();
			}
		});
	},

	/**
	 * 采购暂停
	 */
	dialog_stop: function (url, title, xy) {
		var result = Common.result();
		$.getJSON('material.php?do=PurchaseCk.ajax_emp', function(json) {
			if (json.status == 1) {
				$('#dialog_edit').dialog('option', 'buttons', {
					"关闭": function() {
						$(this).dialog("close");
					},

					"提交": function() {
						if (!confirm("确认是否暂停？")) {
							return false;
						}

						var form = $("#form_edit")[0];
						var body = { };

						body.id = form.id.value;
						body.reason = form.reason.value;
						body.status = 5;

						$(this).dialog("close");
						
						var url = 'material.php?do=PurchaseCk.reasonPost';
						var p2 = $.cookie('p2');
						if (p2 != null) {
							url += p2;
						}

						$.post(url, body, function(data) {
							$(result).html(data);
						});
					}
				});

				dialog_edit(url, title, xy);
			} else {
				_error();
			}
		});	
	},

	/**
	 * 采购取消
	 */
	dialog_cancel: function (url, title, xy) {
		var result = Common.result();
		$.getJSON('material.php?do=PurchaseCk.ajax_emp', function(json) {
			if (json.status == 1) {
				$('#dialog_edit').dialog('option', 'buttons', {
					"关闭": function() {
						$(this).dialog("close");
					},

					"提交": function() {
						if (!confirm("确认是否取消？")) {
							return false;
						}

						var form = $("#form_edit")[0];

						var body = { };

						body.id = form.id.value;
						body.content = form.content.value;
						body.status = 6;

						$(this).dialog("close");
						
						var url = 'material.php?do=PurchaseCk.reasonPost';
						var p2 = $.cookie('p2');
						if (p2 != null) {
							url += p2;
						}

						$.post(url, body, function(data) {
							$(result).html(data);
						});
					}
				});

				dialog_edit(url, title, xy);
			} else {
				_error();
			}
		});	
	},

	expunge_: function (form) {
		$.getJSON('material.php?do=PurchaseCk.audit&item=52', function(json) {
			if (json.status == 1) {
				expunge(form);
			} else {
				_error();
			}
		});
	},
	
	dialog_out: function (url, title, xy) {
		var result = Common.result();
		$('#dialog_add').dialog('option', 'buttons', {
			"关闭": function() { 
				$(this).dialog("close");
			},
			
			"提交": function() {
				if (!confirm("确认是否添加支出信息？")) {
					return false;
				}

				var form = $("#form_add")[0];
						
				var body = { };
				body.id = form.id.value;
				body.supplier = form.supplier.value;				
				body.supplier_x = Supplier.supplier_x(form);
				
				body.name = form.name.value;
				body.type = form.type.value;
				
				body.currency = form.currency.value;
				body.amount = form.amount.value;
				body.rate = form.rate.value;
				body.rmb = form.rmb.value;
				
				body.date = form.date.value;
				body.emp = form.emp.value;
				body.content = form.content.value;

				$(this).dialog("close");
				
				var url = 'material.php?do=PurchaseCk.outPost';
				var p2 = $.cookie('p2');
				if (p2 != null) {
					url += p2;
				}

				$.post(url, body, function(data) {
					$(result).html(data);
				});
			}
		});

		dialog_add(url, title, xy);
	},
	
	popup_all: function (e, p2) {
		p2 = query(p2);
		$.cookie('p2', p2);

		var c1 = 'material.php?do=PurchaseCk.delete' + p2;		
		var c8 = 'material.php?do=PurchaseCk.xls_record' + p2;
		var c9 = 'material.php?do=PurchaseCk.xls' + p2;	
			
		var a1 = ['删除信息', 'url', 'Common.list_delete'];				
		var a8 = ['导出明细Excel', 'url', 'jswin', '1024x768'];
		var a9 = ['导出Excel', 'url', 'jswin', '1024x768'];

		var s = c1 + ", " + c8 + ", " + c9;

		return popup(e, [a1, a8, a9], s);
	},

	list_: function () {
		var result = Common.result();
		var url = 'material.php?do=PurchaseCk.findPost';

		var p2 = $.cookie('p2');
		if (p2 != null) {
			url += p2;
		}

		$.get(url, function(data) {
			$(result).html(data);
		});
	}
};
